const express = require("express");
const router = express.Router();
const multer = require("multer");
const path = require("path");
const {
  getDish,
  addDish,
  deleteDish,
  onSale1,
  getDishDetail,
  addComment,
} = require("../utils/sql");

// 设置 multer 的存储方式为磁盘存储
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, path.join(__dirname, "../../public/images"));
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname); // 设置文件名
  },
});

const upload = multer({ storage: storage });

// 参数 id
router.post("/dishlist", async (req, res) => {
  const { id } = req.body;
  console.log(id);
  const result = await getDish(id);
  console.log(result);
  if (result.length > 0) {
    res.send({ code: 200, msg: "获取成功", data: result });
  } else {
    res.send({ code: 400, msg: "获取失败" });
  }
});

// 参数    dish_name: "",
//  price: "",
//  image_url: "",
//  description: "",
//  restaurant_id: 根据id
router.post("/addDish", upload.single("image"), async (req, res) => {
  const { dish_name, price, description, restaurant_id } = req.body;
  const image_url = req.file.filename;
  const result = await addDish({
    dish_name,
    price,
    image_url,
    description,
    restaurant_id,
  });
  if (result.affectedRows > 0) {
    res.send({ code: 200, msg: "添加成功" });
  } else {
    res.send({ code: 400, msg: "添加失败" });
  }
});

// deleteDish
// 参数 id
router.post("/deleteDish", async (req, res) => {
  const { id } = req.body;
  const result = await deleteDish(id);
  if (result.affectedRows > 0) {
    res.send({ code: 200, msg: "删除成功" });
  } else {
    res.send({ code: 400, msg: "删除失败" });
  }
});

router.post("/toggleOnSale", async (req, res) => {
  const { id, onSale } = req.body;
  console.log(req.body);
  const result = await onSale1(id, onSale);
  if (result.affectedRows > 0) {
    res.send({ code: 200, msg: "上架成功" });
  } else {
    res.send({ code: 400, msg: "上架失败" });
  }
});

// getDishDetail

router.post("/getDishDetail", async (req, res) => {
  const { id } = req.body;
  console.log(id);
  const result = await getDishDetail(id);
  if (result.length > 0) {
    res.send({ code: 200, msg: "获取成功", data: result[0] });
  } else {
    res.send({ code: 400, msg: "获取失败" });
  }
});

// addComments
router.post("/addComments", async (req, res) => {
  const { user_id, dish_id, rating, comment } = req.body;
  console.log(req.body);

  const currentDate = new Date();

  // 格式化日期
  const year = currentDate.getFullYear();
  const month = String(currentDate.getMonth() + 1).padStart(2, "0"); // 月份从0开始，需要加1
  const day = String(currentDate.getDate()).padStart(2, "0");

  // 格式化时间
  const hours = String(currentDate.getHours()).padStart(2, "0");
  const minutes = String(currentDate.getMinutes()).padStart(2, "0");
  const seconds = String(currentDate.getSeconds()).padStart(2, "0");

  // 构造日期时间字符串
  const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

  const result = await addComment(
    user_id,
    dish_id,
    rating,
    comment,
    formattedDateTime
  );
  if (result.affectedRows > 0) {
    res.send({ code: 200, msg: "评论成功" });
  } else {
    res.send({ code: 400, msg: "评论失败" });
  }
});

module.exports = router;
